﻿using OnlineMealOrdering.Common;
using OnlineMealOrdering.Domain;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace OnlineMealOrdering.Models
{
    public class FoodCatalogModel : ModelBase
    {
        public int Id { get; set; }

        public string Name { get; set; }

        public int? ParentId { get; set; }

        public List<FoodModel> Foods { get; set; }

        public static ICollection<FoodCatalogModel> GetAllCatalogs()
        {
            var query = Ctx.EntitySet<FoodCatalogEntity>().Where(a => a.Parent == null);
            ICollection<FoodCatalogModel> list = new List<FoodCatalogModel>();
            if (query.Count() > 0)
            {
                query.ToList().ForEach(a =>
                {
                    list.Add(a.ToFoodCatalogMdoel());
                });
            }
            return list;

        }

        public void Update(int id, string name, int sortNo = 0, bool enable = true)
        {
            var entity = Ctx.EntitySet<FoodCatalogEntity>().SingleOrDefault(a => a.Id == id);
            if (entity == null)
                throw new BusinessException(string.Format("目录(Id:{0})不存在", id));
            entity.Update(id, name, sortNo, enable);
        }

    }
}